<template>
	<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk"
	 @cancel="handleCancel" cancelText="关闭">
		<a-spin :spinning="confirmLoading">
			<a-form :form="form">
				<!-- <a-form-item label="行号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['rowNum']" placeholder="请输入行号"></a-input>
        </a-form-item> -->
				<a-form-item label="料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<j-popup  v-decorator="['materielCode', validatorRules.materielCode]" :trigger-change="true" org-fields="id,materiel_code,product_name,gauge,materiel_type,unit,query2,product_code"
					 dest-fields="materielId,materielCode,materielName,materielGauge,type,unit,query2,product_code" code="materiel_search" @callback="popupCallback" />
				</a-form-item>
				<a-form-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input  v-decorator="['materielName', validatorRules.materielName]" placeholder="请输入物料"></a-input>
				</a-form-item>
				<a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input  v-decorator="['materielGauge', validatorRules.materielGauge]" placeholder="请输入规格"></a-input>
				</a-form-item>
				<a-form-item label="订单数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['orderNum', validatorRules.orderNum]" placeholder="请输入订单数量"></a-input>
				</a-form-item>
				<a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input  v-decorator="['unit', validatorRules.unit]" placeholder="请输入单位"></a-input>
				</a-form-item>
        <a-form-item label="客户料号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientCode']" placeholder="请输入客户料号"></a-input>
        </a-form-item>
        <a-form-item label="商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['productCode']" placeholder="请输入商品编码"></a-input>
        </a-form-item>
				<a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['description']" placeholder="请输入描述"></a-input>
				</a-form-item>
				<!-- <a-form-item label="客户物料编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['clientMaterielcode']" placeholder="请输入客户物料编号"></a-input>
				</a-form-item>
				<a-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
					<a-input v-decorator="['category']" placeholder="请输入类别"></a-input>
				</a-form-item> -->
				<template v-if="!isAdd">
					<a-form-item label="首个日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<j-date placeholder="请选择首个日期" v-decorator="['firstDate']" :trigger-change="true" style="width: 100%" />
					</a-form-item>
					<a-form-item label="工厂编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<!-- <j-popup
							v-decorator="['factoryCode']"
							:trigger-change="true"
							org-fields="id,fac_code,fac_nameone"
							dest-fields="factoryId,factoryCode,factoryName"
							code="factory_search"
							@callback="popupCallback"/> -->
						<j-popup v-decorator="['factoryCode']" :trigger-change="true" org-fields="id,fac_code,fac_nameone,store_name"
						dest-fields="factoryId,factoryCode,factoryName,storageSite" code="factorysite_search" @callback="popupCallback" />
					</a-form-item>
					<a-form-item label="工厂名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['factoryName']" placeholder="请输入工厂名称"></a-input>
					</a-form-item>
					<a-form-item label="库存地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['storageSite']" placeholder="请输入库存地点"></a-input>
					</a-form-item>
					<!-- <a-form-item label="批次" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['batchNum']" placeholder="请输入批次"></a-input>
					</a-form-item> -->
					<a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['type']" placeholder="请输入类型"></a-input>
					</a-form-item>
					<a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['account']" placeholder="请输入金额"></a-input>
					</a-form-item>
					<a-form-item label="货币" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<j-dict-select-tag type="list" v-decorator="['currency']" :trigger-change="true" dictCode="current_money"
						placeholder="请选择货币" />
					</a-form-item>
					<a-form-item label="物料标记" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['imgExgFlag']" placeholder="请输入物料标记"></a-input>
					</a-form-item>
					<!-- <a-form-item label="项号" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['itemNo']" placeholder="请输入项号"></a-input>
					</a-form-item>
					<a-form-item label="批次号" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['sectionNo']" placeholder="请输入批次号"></a-input>
					</a-form-item>
					<a-form-item label="公司编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['companyCode']" placeholder="请输入公司编码"></a-input>
					</a-form-item> -->
					<a-form-item label="板数" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['palletNum']" placeholder="请输入板数"></a-input>
					</a-form-item>
					<a-form-item label="件数" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['pcs',]" placeholder="请输入件数"></a-input>
					</a-form-item>
					<a-form-item label="包装种类" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['wrap']" placeholder="请输入包装种类"></a-input>
					</a-form-item>
					<a-form-item label="箱号" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['containerNo']" placeholder="请输入箱号"></a-input>
					</a-form-item>
					<a-form-item label="净重" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['netWeight']" placeholder="请输入净重"></a-input>
					</a-form-item>
					<a-form-item label="毛重" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['grossWeight']" placeholder="请输入毛重"></a-input>
					</a-form-item>
					<a-form-item label="立方数" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['zextendItemField1']" placeholder="请输入立方数"></a-input>
					</a-form-item>
					<a-form-item label="目的国" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-decorator="['destinationCountry']" placeholder="请输入目的国"></a-input>
					</a-form-item>
					<a-form-item label="票据日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-date placeholder="请输入票据日期yyyy/m/d 默认报关当天" v-decorator="['companyCode']" style="width: 100%"></j-date>
<!--						<a-input v-decorator="['companyCode']" placeholder="请输入票据日期yyyy/m/d 默认报关当天"></a-input>-->
					</a-form-item>
					<a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<!-- <a-input v-decorator="['state']" placeholder="请输入状态"></a-input> -->
						<j-dict-select-tag type="list" v-decorator="['state']" :trigger-change="true" dictCode="state"
						placeholder="请选择状态" />
					</a-form-item>
					<a-form-item v-show="false" label="物料ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-show="false" v-decorator="['materielId']" placeholder="请输入物料ID"></a-input>
					</a-form-item>
					<a-form-item v-show="false" label="工厂ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
						<a-input v-show="false" v-decorator="['factoryId']" placeholder="请输入工厂ID"></a-input>
					</a-form-item>
				</template>
			</a-form>
		</a-spin>
	</j-modal>
</template>

<script>
	import {
		httpAction
	} from '@/api/manage'
	import pick from 'lodash.pick'
	import {
		validateDuplicateValue
	} from '@/utils/util'
	import JDate from '@/components/jeecg/JDate'
	import JDictSelectTag from "@/components/dict/JDictSelectTag"

	export default {
		name: "MesSaleItemModal",
		components: {
			JDate,
			JDictSelectTag,
		},
		props: {
			mainId: {
				type: String,
				required: false,
				default: ''
			},
			isAdd: {
				type: Boolean,
				required: false,
				default: false
			}
		},
		data() {
			return {
				form: this.$form.createForm(this),
				title: "操作",
				width: 800,
				visible: false,
				model: {},
				labelCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 5
					},
				},
				wrapperCol: {
					xs: {
						span: 24
					},
					sm: {
						span: 16
					},
				},

				confirmLoading: false,
				validatorRules: {
					materielCode: {
						rules: [{
							required: true,
							message: '请选择料号!'
						}, ]
					},
					materielName: {
						rules: [{
							required: true,
							message: '请输入物料!'
						}, ]
					},
					materielGauge: {
						rules: [{
							required: true,
							message: '请输入规格!'
						}, ]
					},
          orderNum: {
          	rules: [{
          		required: true,
          		message: '订单数量不能为空!'
          	}, ]
          },
					unit: {
						rules: [{
							required: true,
							message: '请输入单位!'
						}, ]
					},
					factoryCode: {
						rules: [{
							required: true,
							message: '请选择工厂编号!'
						}, ]
					},
					factoryName: {
						rules: [{
							required: true,
							message: '请输入工厂地点!'
						}, ]
					},
					storageSite: {
						rules: [{
							required: true,
							message: '请输入库存地点!'
						}, ]
					},
					type: {
						rules: [{
							required: true,
							message: '请输入类型!'
						}, ]
					},
					account: {
						rules: [{
							required: true,
							message: '请输入金额!'
						}, ]
					},
          imgExgFlag: {
          	rules: [{
          		required: true,
          		message: '请输入物料标记!'
          	}, ]
          },
          pcs: {
          	rules: [{
          		required: true,
          		message: '请输入件数!'
          	}, ]
          },
          wrap: {
          	rules: [{
          		required: true,
          		message: '请输入包装种类!'
          	}, ]
          },
          netWeight: {
          	rules: [{
          		required: true,
          		message: '请输入净重!'
          	}, ]
          },
          grossWeight: {
          	rules: [{
          		required: true,
          		message: '请输入毛重!'
          	}, ]
          },
          zextendItemField1: {
          	rules: [{
          		required: true,
          		message: '请输入立方数!'
          	}, ]
          },
          destinationCountry: {
          	rules: [{
          		required: true,
          		message: '请输入目的国!'
          	}, ]
          },
          companyCode: {
          	rules: [{
          		required: true,
          		message: '请输入票据日期!'
          	}, ]
          },
          state: {
          	rules: [{
          		required: true,
          		message: '请输入状态!'
          	}, ]
          },
				},
				url: {
					add: "/order/mesOrderSale/addMesSaleItem",
					edit: "/order/mesOrderSale/editMesSaleItem",
				}

			}
		},
		created() {},
		methods: {
			add() {
				this.edit({});
			},
			edit(record) {
			  console.log(11,record)
				this.form.resetFields();
				this.model = Object.assign({}, record);
				this.visible = true;
				this.$nextTick(() => {
					this.form.setFieldsValue(pick(this.model, 'createBy', 'createTime', 'updateBy', 'updateTime', 'sysOrgCode',
						'materielId', 'materielCode', 'materielName', 'materielGauge', 'orderNum', 'unit', 'description',
						'clientMaterielcode', 'category', 'firstDate', 'factoryId', 'factoryCode', 'factoryName', 'storageSite',
						'batchNum', 'type', 'account', 'currency', 'state','saleorderId', 'rowNum','imgExgFlag','itemNo','sectionNo','companyCode','palletNum','pcs','zextendItemField1','destinationCountry','wrap','containerNo','netWeight','grossWeight','query3', 'query4', 'query5', 'query6','productCode','clientCode'))
          this.form.setFieldsValue({clientCode:record.clientCode,productCode:record.productCode})
				})
			},
			close() {
				this.$emit('close');
				this.visible = false;
			},
			handleOk() {
				const that = this;
				// 触发表单验证
				this.form.validateFields((err, values) => {
					if (!err) {
						that.confirmLoading = true;
						let httpurl = '';
						let method = '';
						if (!this.model.id) {
							httpurl += this.url.add;
							method = 'post';
						} else {
							httpurl += this.url.edit;
							method = 'put';
						}
						let formData = Object.assign(this.model, values);
						formData['saleorderId'] = this.mainId
						console.log("表单提交数据", formData)
						httpAction(httpurl, formData, method).then((res) => {
							if (res.success) {
								that.$message.success(res.message);
								that.$emit('ok');
							} else {
								that.$message.warning(res.message);
							}
						}).finally(() => {
							that.confirmLoading = false;
							that.close();
						})
					}

				})
			},
			handleCancel() {
				this.close()
			},
			popupCallback(row) {
			  console.log(row,111)

				this.form.setFieldsValue(pick(row, 'createBy', 'createTime', 'updateBy', 'updateTime', 'sysOrgCode', 'materielId',
					'materielCode', 'materielName', 'materielGauge', 'orderNum', 'unit', 'description', 'clientMaterielcode',
					'category', 'firstDate', 'factoryId', 'factoryCode', 'factoryName', 'storageSite', 'batchNum', 'type', 'account',
					'currency', 'state', 'saleorderId', 'rowNum','imgExgFlag','itemNo','sectionNo','companyCode','palletNum','pcs','zextendItemField1','destinationCountry','wrap','containerNo','netWeight','grossWeight','query3', 'query4', 'query5', 'query6','productCode','clientCode'))
        this.form.setFieldsValue({clientCode:row.query2,productCode:row.product_code})
      },


		}
	}
</script>
